package org.neframework.webtest.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.neframework.webtest.model.t_fk_log;
import org.neframework.webtest.model.t_fk_log.Level;
import org.neframework.webtest.util.web.session.SessionTools;

import com.tmsps.neframework.core.common.page.Page;
import com.tmsps.neframework.jpa.base.BaseService;
import com.tmsps.neframework.jpa.sql.param.NeParamList;
import com.tmsps.neframework.mvc.utils.ip.IPTools;

public class LogService extends BaseService {

	private HttpServletRequest req;

	public LogService(HttpServletRequest req) {
		this.req = req;
	}

	public static void main(String[] args) {
		// LogService s = new LogService();
	}

	public void logH(String logStr, Class<?> clazz, String clazz_id) {
		log(logStr, clazz, clazz_id, Level.H);
	}

	public void logM(String logStr, Class<?> clazz, String clazz_id) {
		log(logStr, clazz, clazz_id, Level.M);
	}

	public void logL(String logStr, Class<?> clazz, String clazz_id) {
		log(logStr, clazz, clazz_id, Level.L);
	}

	public void logLL(String logStr, String user_id) {
		t_fk_log log = new t_fk_log();
		log.setLog(logStr);
		log.setUser_id(user_id);
		log.setLevel(Level.L.toString());
		// log.setCompany_id(WebTools.getCurrentBankId());
		oc.saveObj(log);
	}

	public void log(String logStr, Class<?> clazz, String clazz_id, Level level) {
		t_fk_log log = new t_fk_log();
		log.setLog(logStr);
		log.setClazz(clazz.toString());
		log.setClazz_id(clazz_id);
		log.setUser_id(SessionTools.getCurrentLoginAdmin(req).getKid());
		log.setLevel(level.toString());
		log.setIp(IPTools.getIp(req));
		oc.saveObj(log);
	}

	public void logLL(String logStr) {
		logLL(logStr, null);
	}
	
	public List<Map<String, Object>> selectLogList(String srh_log, Map<String, String> sort_params, Page page) {
		// TODO 查询日志
		String sql = "select * from t_fk_log t where t.status=0 and (t.log like ?)";

		NeParamList params = NeParamList.makeParams();
		params.addLike(srh_log);

		List<Map<String, Object>> list = jcn.queryForList(sql, params, sort_params, page);
		return list;
	}
}
